﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 第三章
{
    //声明委托
    public delegate bool ValidateDlg(DateTime birthday);

    public class UserInfo1 : System.Web.UI.Page
    {
        //声明事件
        private static event ValidateDlg ValidateEvent;

        string Name;
        DateTime Birthday;

        public UserInfo1(string Name, DateTime Birthday)
        {
            this.Name = Name;
            this.Birthday = Birthday;
        }

        public string DecideAge()
        {
            //注册事件
            ValidateEvent += ValidateBiethday;

            //触发事件
            if (ValidateEvent != null)
            {
                if (ValidateBiethday(this.Birthday))
                {
                    return "生日范围错误，请重新选择！";
                }
            }
            if (DateTime.Now.Year - this.Birthday.Year > 18)
            {
                return "您是成人了！";
            }
            else
            {
                return "您还没长大呢！";
            }
        }

        //事件函数
        private bool ValidateBiethday(DateTime birthday)
        {
            return this.Birthday < DateTime.Parse("1900-1-1") || this.Birthday > DateTime.Now;
        }
    }


    public partial class _3__9 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            string name = this.TXTname.Text;
            DateTime birthday;
            try
            {
                birthday = DateTime.Parse(this.InputBirthday.Value);
            }
            catch (Exception)
            {
                Response.Write("请重新输入年龄！");
                return;
            }
            UserInfo1 use = new UserInfo1(name, birthday);
            Response.Write(use.DecideAge());
        }
    }
}